#include "SequenceWidget.h"
#include "Sequence.h"

namespace vkbrd
{
	SequenceWidget::SequenceWidget(QWidget* _parent) :
QWidget(_parent)
{
	ui.setupUi(this);

	fingers << ui.fingerCheckBox_0 << 
		ui.fingerCheckBox_1 << 
		ui.fingerCheckBox_2 << 
		ui.fingerCheckBox_3 << 
		ui.fingerCheckBox_4 << 
		ui.fingerCheckBox_5 << 
		ui.fingerCheckBox_6 << 
		ui.fingerCheckBox_7 << 
		ui.fingerCheckBox_8 << 
		ui.fingerCheckBox_9;

	QMap<int, QString>::iterator it;
	for (it = Sequence::vKeyNames.begin(); it != Sequence::vKeyNames.end(); ++it)
		ui.comboBox->addItem(*it, it.key());

	///@todo connect widget to save current profile
	bool succ = true;
	foreach(QCheckBox* finger, fingers)
		succ &= connect(finger, SIGNAL(toggled(bool)), this, SIGNAL(sequenceChanged()));

	succ &= connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(sequenceChanged()));
	Q_ASSERT(succ);
}

vkbrd::Sequence SequenceWidget::getSequence()
{
	int combination = 0;
	
	for(int i = 0; i < 10; ++i)
		if (fingers[i]->isChecked())
			combination |= 1 << i;

	return Sequence(combination, ui.comboBox->itemData(ui.comboBox->currentIndex()).toInt());
}

void SequenceWidget::setSequence(Sequence _seq)
{
	for(int i = 0; i < 10; ++i)
		fingers[i]->setChecked(_seq.getCombination() & (1 << i));

	ui.comboBox->setCurrentIndex(ui.comboBox->findData(_seq.getAction()));
}

}
